using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using Utilities.Menu.UIElements;
using EnhancedTimer.Resources;
using Services.Menu;
using Utilities;
using Microsoft.Phone.Shell;

namespace EnhancedTimer
{
    using SU = StaticUtilities;
    class OptionsMenu : DrawableGameComponent
    {
        ArrayList<MyButton> strings;
        
        ArrayList<MyButton> alertsButton;
        MyButton confirm, confirmAlertSound;
        MyButton soundSwitch, vibrationSwitch, allowRunSwitch, alarmSoundButton;
        MyButton soundString, vibrationString, allowRunString;
        InputManager input_manager;
        SpriteBatch spriteBatch;
        StateManager StateManager;
        protected float press_wait;
        Texture2D switch_off, switch_on;
        protected readonly float PRESS_INTERVAL = 0.5f;
        bool showAlertChoise;
        int selectedAlertSound;
        public OptionsMenu(Game game)
            : base(game)
        {
        }

        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            StateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            Game.Services.RemoveService(typeof(IInputManager));
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            strings = new ArrayList<MyButton>();
            switch_off = Game.Content.Load<Texture2D>("switch_off");
            switch_on = Game.Content.Load<Texture2D>("switch_on");
            var font = Game.Content.Load<SpriteFont>("Fonts/SegoeWP_Settings");
            var yesNoFont = Game.Content.Load<SpriteFont>("Fonts/SegoeWP");
            var titleFont = Game.Content.Load<SpriteFont>("Fonts/SegoeWP_Title");
            var box = Game.Content.Load<Texture2D>("bar_box");
            var whiteTexture = Game.Content.Load<Texture2D>("whitePixel");
            
            strings.Add(new MyButton(new Rectangle(0, 0, 480, 120), Strings.Settings, Color.White, titleFont));
            strings.Add(new MyButton(new Rectangle(0, 120, 480, 80), Strings.Sound, Color.White, font, TextPos.Left));
            strings.Add(new MyButton(new Rectangle(0, 245, 480, 80), Strings.Vibration, Color.White, font, TextPos.Left));
            //strings.Add(new MyButton(new Rectangle(0, 370, 480, 80), Strings.AllowRunOffScreen, Color.White, font, TextPos.Left));
            //strings.Add(new MyButton(new Rectangle(0, 410, 480, 60), Strings.AllowRunOffScreen2, Color.White, font, TextPos.Left));
            strings.Add(new MyButton(new Rectangle(0, 520, 480, 80), Strings.AlarmSound, Color.White, font, TextPos.Left));

            confirm = new MyButton(new Rectangle(260, 720, 180, 60), Strings.Confirm, Color.White, yesNoFont, TextPos.Center, box, Color.White);

            soundSwitch = new MyButton(new Rectangle(330, 200, 100, 40), StateManager.OptionsState.sound ? switch_on : switch_off, Color.White);

            vibrationSwitch = new MyButton(new Rectangle(330, 320, 100, 40), StateManager.OptionsState.vibration ? switch_on : switch_off, Color.White);

            //allowRunSwitch = new MyButton(new Rectangle(330, 470, 100, 40), StateManager.OptionsState.allowRunScreenOff ? switch_on : switch_off, Color.White);

            alarmSoundButton = new MyButton(new Rectangle(10, 600, 460, 60), "", Color.Green, font, TextPos.Left, whiteTexture, Color.LightGray);

            soundString = new MyButton(new Rectangle(0, 200, 100, 60), StateManager.OptionsState.sound ? Strings.On : Strings.Off, Color.White, yesNoFont);

            vibrationString = new MyButton(new Rectangle(0, 320, 100, 60), StateManager.OptionsState.vibration ? Strings.On : Strings.Off, Color.White, yesNoFont);

            //allowRunString = new MyButton(new Rectangle(0, 470, 100, 60), StateManager.OptionsState.allowRunScreenOff ? Strings.On : Strings.Off, Color.White, yesNoFont);

            showAlertChoise = false;

            alertsButton = new ArrayList<MyButton>();

            
            selectedAlertSound = StateManager.OptionsState.alertSound;
            for (int i=0;i<StateManager.Sounds.Count;i++)
            {
                alertsButton.Add(new MyButton(new Rectangle(10, 50 + (50 + 10) * i, 460, 50), "Alert " + i, Color.White, yesNoFont, TextPos.Left));
            }
            if (selectedAlertSound > alertsButton.Count || selectedAlertSound < 0)
                selectedAlertSound = 0;
            alertsButton[selectedAlertSound].textColor = Color.Green;
            alarmSoundButton.text = "Alert " + (selectedAlertSound+1);
            alarmSoundButton.UpdateTextPos();
            confirmAlertSound = new MyButton(new Rectangle(260, 720, 180, 60), Strings.ConfirmSound, Color.White, yesNoFont, TextPos.Center, box, Color.White);
            
            base.LoadContent();
        }
        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IInputManager));
            base.Dispose(disposing);
        }
        public override void Update(GameTime gameTime)
        {
            if (press_wait < PRESS_INTERVAL)
                press_wait += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                if (showAlertChoise)
                {
                    closeAlertChoise();
                }
                else
                {
                    Storage.SaveOptionsState(StateManager.OptionsState);
                    StateManager.Resume(false);
                }
            } 
            base.Update(gameTime);
        }

        
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            
            spriteBatch.Begin();
            if (!showAlertChoise)
            {
                for (int i = 0; i < strings.Count; i++)
                    strings[i].Draw(spriteBatch);
                soundSwitch.Draw(spriteBatch);
                vibrationSwitch.Draw(spriteBatch);
                //allowRunSwitch.Draw(spriteBatch);
                alarmSoundButton.Draw(spriteBatch);
                confirm.Draw(spriteBatch);
                soundString.Draw(spriteBatch);
                vibrationString.Draw(spriteBatch);
                //allowRunString.Draw(spriteBatch);
            }
            else
            {
                for (int i = 0; i < alertsButton.Count; i++)
                    alertsButton[i].Draw(spriteBatch);
                confirmAlertSound.Draw(spriteBatch);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
        //Point tp = new Point();
        void on_tap(Vector2 tap_position)
        {
            if (press_wait < PRESS_INTERVAL) return;
            //tp.X = (int)tap_position.X;
            //tp.Y = (int)tap_position.Y;
            if (!showAlertChoise)
            {
                if (confirm.tapped_on(tap_position))
                {
                    Storage.SaveOptionsState(StateManager.OptionsState);
                    StateManager.Resume(false);
                }
                else if (soundSwitch.tapped_on(tap_position))
                {
                    StateManager.OptionsState.sound = !StateManager.OptionsState.sound;
                    if (StateManager.OptionsState.sound)
                        soundSwitch.texture = switch_on;
                    else
                        soundSwitch.texture = switch_off;
                    UpdateStringButton(soundString, StateManager.OptionsState.sound);
                }
                else if (vibrationSwitch.tapped_on(tap_position))
                {
                    StateManager.OptionsState.vibration = !StateManager.OptionsState.vibration;
                    if (StateManager.OptionsState.vibration)
                        vibrationSwitch.texture = switch_on;
                    else
                        vibrationSwitch.texture = switch_off;
                    UpdateStringButton(vibrationString, StateManager.OptionsState.vibration);
                }
                /*else if (allowRunSwitch.tapped_on(tap_position))
                {
                    StateManager.OptionsState.allowRunScreenOff = !StateManager.OptionsState.allowRunScreenOff;
                    if (StateManager.OptionsState.allowRunScreenOff)
                        allowRunSwitch.texture = switch_on;
                    else
                        allowRunSwitch.texture = switch_off;
                    UpdateStringButton(allowRunString, StateManager.OptionsState.allowRunScreenOff);
                    if (StateManager.OptionsState.allowRunScreenOff)
                        PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
                    else
                        PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Enabled;
                }*/
                else if (alarmSoundButton.tapped_on(tap_position))
                {
                    showAlertChoise = true;
                }
            }
            else
            {
                if (confirmAlertSound.tapped_on(tap_position))
                {
                    closeAlertChoise();
                }
                else
                {
                    for (int i = 0; i < alertsButton.Count; i++)
                        if (alertsButton[i].tapped_on(tap_position))
                        {

                            for (int j = 0; j < alertsButton.Count; j++)
                            {
                                alertsButton[j].textColor = Color.White;
                                StateManager.Sounds[i].Stop();
                            }

                            StateManager.Sounds[i].Play();
                            alertsButton[i].textColor = Color.Green;
                            selectedAlertSound = i;
                        }
                }
            }
        }
        public void UpdateStringButton(MyButton b, bool val)
        {
            if (val)
                b.text = Strings.On;
            else
                b.text = Strings.Off;
        }
        public void closeAlertChoise()
        {
            showAlertChoise = false;
            StateManager.OptionsState.alertSound = selectedAlertSound;
            alarmSoundButton.text = "Alert " + (selectedAlertSound + 1);
            alarmSoundButton.UpdateTextPos();
            StateManager.Sounds[selectedAlertSound].Stop();
        }
    }
}
